<!--
 *登录组件
 * @Author: 舒克
 * @Official Accounts: 氧化1028
 * @email: 143587623@qq.com
 * @github: https://gitee.com/KingSio
 * @Date: 2020-12-05 20:43:13
 * @LastEditTime: 2021-01-15 16:26:53
 * @FilePath: \Vue全栈后台\项目\vue_shop\src\components\Login.vue
-->

<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单区域 rules，绑定一个表单验证规则-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!-- 按钮提交 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="LoginForm">登录</el-button>
          <el-button type="info" @click="reseLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>

import {Login} from "@/api/login";
export default {
  data() {
    return {
      //登录表单的数据绑定
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //表单验证规则对象
      loginFormRules: {
        //验证用户名，trigger鼠标离开触发验证
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        //验证密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //重置登录表单
    reseLoginForm() {
      // resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.loginFormRef.resetFields()
    },
    //登录
    LoginForm() {
      //获取表单数据
      this.$refs.loginFormRef.validate(async val => {
        if (!val) {
          return
        } else {
          //const result=await this.$http.post("login",this.loginFrom);
          //console.log(result.data)
          //data是后端的数据，进行解构，，把他重命名为res,
        let loginData={...this.loginForm}
        const {data:res}=await Login(loginData)
        //旧的请求
        //  const { data: res } = await this.$http.post('login', this.loginForm)
          
      if (res.meta.status !== 200) {
            return this.$message.error('登录失败')
          } else {
            this.$message.success('登录成功')
            //1.2token只应在当前网站打开有效，所以保存在sessionStorage
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('/home')
          } 
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: red;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  border-radius: 3px;
  background-color: white;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid white;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    top: 0;
    transform: translate(-50%, -50%);
    background-color: white;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box; /* padding(内边距) 和 border(边框) 也包含在 width 和 height 中 */
}
.btns {
  display: left;
  justify-content: flex-end;
}
</style>
